<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>MongoDB 类</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongoclient.tostring.html">MongoClient::__toString</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongodb.authenticate.html">MongoDB::authenticate</a></div>
 <div class="up"><a href="mongo.core.html">核心类</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="class.mongodb" class="reference">

 <h1 class="title">MongoDB 类</h1>
 

 <div class="partintro"><p class="verinfo">(PECL mongo &gt;=0.9.0)</p>


  <div class="section" id="mongodb.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    该类的实例用于和数据库进行交互。要获取一个数据库：
    <div class="example" id="example-1379">
     <p><strong>Example #1 选择一个数据库</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();&nbsp;</span><span style="color: #FF8000">//&nbsp;连接<br /></span><span style="color: #0000BB">$db&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">selectDB</span><span style="color: #007700">(</span><span style="color: #DD0000">"example"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
    数据库名可以用 ASCII 范围内的几乎任何字符。
    但是，它们不能包括 &quot; &quot;、&quot;.&quot;，或者是空字符串。
    名称 &quot;system&quot; 也是被保留的。
   </p>
   <p class="para">
    个别特殊但有效的数据库名：&quot;null&quot;、&quot;[x,y]&quot;、&quot;3&quot;、&quot;\&quot;&quot;、 &quot;/&quot;。
   </p>
   <p class="para">
    不像集合名，数据库名是可以包含 &quot;$&quot; 的。
   </p>
  </div>


  <div class="section" id="mongodb.synopsis">
   <h2 class="title">类摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass"></div>


    <div class="classsynopsisinfo">
     <span class="ooclass">
      <strong class="classname">MongoDB</strong>
     </span>
     {</div>

    
    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 常量 */</div>
    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">int</span>
      <var class="fieldsynopsis_varname"><a href="class.mongodb.html#mongodb.constants.profilingoff"><var class="varname">PROFILING_OFF</var></a></var>
     <span class="initializer"> = 0</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">int</span>
      <var class="fieldsynopsis_varname"><a href="class.mongodb.html#mongodb.constants.profilingslow"><var class="varname">PROFILING_SLOW</var></a></var>
     <span class="initializer"> = 1</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">int</span>
      <var class="fieldsynopsis_varname"><a href="class.mongodb.html#mongodb.constants.profilingon"><var class="varname">PROFILING_ON</var></a></var>
     <span class="initializer"> = 2</span>
    ;</div>


    <div class="classsynopsisinfo classsynopsisinfo_comment">/* Fields */</div>
    <div class="fieldsynopsis">
     <span class="modifier">public</span>
     <span class="type">integer</span>
      <var class="varname"><a href="class.mongodb.html#mongodb.props.w">$<var class="varname">w</var></a></var>
     <span class="initializer"> = 1</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">public</span>
     <span class="type">integer</span>
      <var class="varname"><a href="class.mongodb.html#mongodb.props.wtimeout">$<var class="varname">wtimeout</var></a></var>
     <span class="initializer"> = 10000</span>
    ;</div>


    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="mongodb.authenticate.html" class="methodname">authenticate</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$username</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$password</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="mongodb.command.html" class="methodname">command</a></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$command</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code><span class="initializer"> = array()</span></span>
  ] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="mongodb.construct.html" class="methodname">__construct</a></span>
    ( <span class="methodparam"><span class="type"><a href="class.mongoclient.html" class="type MongoClient">MongoClient</a></span> <code class="parameter">$conn</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">MongoCollection</span><span class="methodname"><a href="mongodb.createcollection.html" class="methodname">createCollection</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code></span>
  ] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="mongodb.createdbref.html" class="methodname">createDBRef</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$collection</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$document_or_id</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="mongodb.drop.html" class="methodname">drop</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="mongodb.dropcollection.html" class="methodname">dropCollection</a></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$coll</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="mongodb.execute.html" class="methodname">execute</a></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$code</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$args</code><span class="initializer"> = array()</span></span>
  ] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">bool</span><span class="methodname"><a href="mongodb.forceerror.html" class="methodname">forceError</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">MongoCollection</span><span class="methodname"><a href="mongodb.get.html" class="methodname">__get</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="mongodb.getcollectionnames.html" class="methodname">getCollectionNames</a></span>
    ([ <span class="methodparam"><span class="type">bool</span> <code class="parameter">$includeSystemCollections</code><span class="initializer"> = false</span></span>
  ] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="mongodb.getdbref.html" class="methodname">getDBRef</a></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$ref</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">MongoGridFS</span><span class="methodname"><a href="mongodb.getgridfs.html" class="methodname">getGridFS</a></span>
    ([ <span class="methodparam"><span class="type">string</span> <code class="parameter">$prefix</code><span class="initializer"> = &quot;fs&quot;</span></span>
  ] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">int</span><span class="methodname"><a href="mongodb.getprofilinglevel.html" class="methodname">getProfilingLevel</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="mongodb.getreadpreference.html" class="methodname">getReadPreference</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">bool</span><span class="methodname"><a href="mongodb.getslaveokay.html" class="methodname">getSlaveOkay</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="mongodb.lasterror.html" class="methodname">lastError</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="mongodb.listcollections.html" class="methodname">listCollections</a></span>
    ([ <span class="methodparam"><span class="type">bool</span> <code class="parameter">$includeSystemCollections</code><span class="initializer"> = false</span></span>
  ] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="mongodb.preverror.html" class="methodname">prevError</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="mongodb.repair.html" class="methodname">repair</a></span>
    ([ <span class="methodparam"><span class="type">bool</span> <code class="parameter">$preserve_cloned_files</code><span class="initializer"> = <strong><code>FALSE</code></strong></span></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$backup_original_files</code><span class="initializer"> = <strong><code>FALSE</code></strong></span></span>
  ]] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="mongodb.reseterror.html" class="methodname">resetError</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">MongoCollection</span><span class="methodname"><a href="mongodb.selectcollection.html" class="methodname">selectCollection</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">int</span><span class="methodname"><a href="mongodb.setprofilinglevel.html" class="methodname">setProfilingLevel</a></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$level</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">bool</span><span class="methodname"><a href="mongodb.setreadpreference.html" class="methodname">setReadPreference</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$read_preference</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$tags</code></span>
  ] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">bool</span><span class="methodname"><a href="mongodb.setslaveokay.html" class="methodname">setSlaveOkay</a></span>
    ([ <span class="methodparam"><span class="type">bool</span> <code class="parameter">$ok</code><span class="initializer"> = true</span></span>
  ] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">string</span><span class="methodname"><a href="mongodb.--tostring.html" class="methodname">__toString</a></span>
    ( <span class="methodparam">void</span>
   )</div>

   }</div>


  </div>

  <div class="section">
   <h2 class="title">预定义常量</h2>
   <div class="section" id="mongodb.constants.types">
    <h2 class="title">MongoDB 日志级别</h2>
    <dl>


     
      <dt id="mongodb.constants.profilingoff"><strong><code>MongoDB::PROFILING_OFF</code></strong></dt>

      <dt>
<strong><code>0</code></strong></dt>

      <dd>

       <span class="simpara">
        关闭了分析器。
       </span>
      </dd>

     
     
      <dt id="mongodb.constants.profilingslow"><strong><code>MongoDB::PROFILING_SLOW</code></strong></dt>

      <dt>
<strong><code>1</code></strong></dt>

      <dd>

       <span class="simpara">
        为慢操作开启了分析器（&gt;100 ms）。
       </span>
      </dd>

     
     
      <dt id="mongodb.constants.profilingon"><strong><code>MongoDB::PROFILING_ON</code></strong></dt>

      <dt>
<strong><code>2</code></strong></dt>

      <dd>

       <span class="simpara">
        为所有操作开启了分析器。
       </span>
      </dd>

     
    </dl>

   </div>
  </div>

  <div class="section">
   <h2 class="title">字段</h2>
   <dl>

    
     <dt id="mongodb.props.w"><var class="varname"><var class="varname">w</var></var></dt>

     <dt>
1</dt>

     <dd>

      <p class="para">
       在返回成功之前，复制修改到此数量的服务器。
       <a href="class.mongocollection.html" class="classname">MongoCollection</a> 实例的设置从这里继承。
       <em>w</em> 仅仅在 MongoDB 服务器版本 1.5.1+ 以及本驱动 1.0.8+ 有效。
      </p>
      <p class="para">
       <em>w</em> 用于你需要调整确认级别时
       （<span class="function"><a href="mongocollection.insert.html" class="function">MongoCollection::insert()</a></span>、
       <span class="function"><a href="mongocollection.update.html" class="function">MongoCollection::update()</a></span>、
       <span class="function"><a href="mongocollection.remove.html" class="function">MongoCollection::remove()</a></span>、
       <span class="function"><a href="mongocollection.save.html" class="function">MongoCollection::save()</a></span> 和
       <span class="function"><a href="mongocollection.ensureindex.html" class="function">MongoCollection::ensureIndex()</a></span> 都支持这个选项）。 
       
       默认值（1）情况下，只要数据库有操作就会确认。
       如果在复制到从服务器前服务器宕机了，它将可能永久丢失本次操作。
       所以，你可以为 <em>w</em> 指定一个比一更高的数字，
       在返回成功之前确保至少一个从服务器完成了操作。
      </p>
      <p class="para">
       例如，如果 <em>w</em> 是 2，主服务器和一个从服务必须记录了本次操作，
       否则驱动会抛出
       <a href="class.mongocursorexception.html" class="classname">MongoCursorException</a>。
       它尝试写入总计
       <em>w</em> 个从服务器 + 主服务器，但是如果其中一个从服务器宕机了，
       操作也会失败，并会抛出异常，所以通常 <em>w=2</em> 是最安全的（主服务器和一个从服务器）。
      </p>
     </dd>

    
    
     <dt id="mongodb.props.wtimeout"><var class="varname"><var class="varname">wtimeout</var></var></dt>

     <dt>
10000</dt>

     <dd>

      <p class="para">
       等待 <em>MongoDB::$w</em> 复制生效的毫秒数。
       <a href="class.mongocollection.html" class="classname">MongoCollection</a> 实例的设置从这里继承。
       <em>w</em> 仅仅在 MongoDB 服务器版本 1.5.1+ 并且驱动版本 1.0.8+ 有效。
      </p>
      <p class="para">
       除非设置了 <em>wtimeout</em>，服务器会永久等待复制到 <em>w</em> 个服务器。
       这个驱动默认会等待 10 秒，你可以修改这个值来改变它的行为。
      </p>
     </dd>

    
   </dl>

  </div>

  <div class="section">
   <h2 class="title">参见</h2>
   <p class="para">
    MongoDB 关于 <a href="http://docs.mongodb.org/manual/reference/glossary/#term-database" class="link external">&raquo;&nbsp;databases</a> 的核心文档。
   </p>
  </div>
 </div>

 











 







 





 





 





 





 





 





 





 












 





 





 












 





 





  





 





 





 





 





 












 





 



<h2>Table of Contents</h2><ul class="chunklist chunklist_reference"><li><a href="mongodb.authenticate.html">MongoDB::authenticate</a> — 登录到数据库</li><li><a href="mongodb.command.html">MongoDB::command</a> — Execute a database command</li><li><a href="mongodb.construct.html">MongoDB::__construct</a> — 选择一个数据库</li><li><a href="mongodb.createcollection.html">MongoDB::createCollection</a> — Creates a collection</li><li><a href="mongodb.createdbref.html">MongoDB::createDBRef</a> — Creates a database reference</li><li><a href="mongodb.drop.html">MongoDB::drop</a> — Drops this database</li><li><a href="mongodb.dropcollection.html">MongoDB::dropCollection</a> — Drops a collection [deprecated]</li><li><a href="mongodb.execute.html">MongoDB::execute</a> — Runs JavaScript code on the database server.</li><li><a href="mongodb.forceerror.html">MongoDB::forceError</a> — Creates a database error</li><li><a href="mongodb.get.html">MongoDB::__get</a> — Gets a collection</li><li><a href="mongodb.getcollectionnames.html">MongoDB::getCollectionNames</a> — Get all collections from this database</li><li><a href="mongodb.getdbref.html">MongoDB::getDBRef</a> — Fetches the document pointed to by a database reference</li><li><a href="mongodb.getgridfs.html">MongoDB::getGridFS</a> — Fetches toolkit for dealing with files stored in this database</li><li><a href="mongodb.getprofilinglevel.html">MongoDB::getProfilingLevel</a> — Gets this database's profiling level</li><li><a href="mongodb.getreadpreference.html">MongoDB::getReadPreference</a> — Get the read preference for this database</li><li><a href="mongodb.getslaveokay.html">MongoDB::getSlaveOkay</a> — Get slaveOkay setting for this database</li><li><a href="mongodb.lasterror.html">MongoDB::lastError</a> — Check if there was an error on the most recent db operation performed</li><li><a href="mongodb.listcollections.html">MongoDB::listCollections</a> — Gets an array of all MongoCollections for this database</li><li><a href="mongodb.preverror.html">MongoDB::prevError</a> — Checks for the last error thrown during a database operation</li><li><a href="mongodb.repair.html">MongoDB::repair</a> — Repairs and compacts this database</li><li><a href="mongodb.reseterror.html">MongoDB::resetError</a> — Clears any flagged errors on the database</li><li><a href="mongodb.selectcollection.html">MongoDB::selectCollection</a> — Gets a collection</li><li><a href="mongodb.setprofilinglevel.html">MongoDB::setProfilingLevel</a> — Sets this database's profiling level</li><li><a href="mongodb.setreadpreference.html">MongoDB::setReadPreference</a> — Set the read preference for this database</li><li><a href="mongodb.setslaveokay.html">MongoDB::setSlaveOkay</a> — Change slaveOkay setting for this database</li><li><a href="mongodb.--tostring.html">MongoDB::__toString</a> — The name of this database</li></ul>
</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongoclient.tostring.html">MongoClient::__toString</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongodb.authenticate.html">MongoDB::authenticate</a></div>
 <div class="up"><a href="mongo.core.html">核心类</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
